/* -----------------------------------------------------------------------------
 * PROJECT    : net.lwf
 * FILE       : WordSoundsHandler.java
 *
 * HISTORY:
 * Date          Author                        Description
 * ----------------------------------------------------------------------------- 
 * Dec 16, 2008       Raghu Sarraff			       Created
 */
package lwf.handler.english;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

import net.lwf.handler.util.WordUtil;

import lwf.factory.PropertyReader;

/**
 * Type the class description here
 *  
 */
public class WordSoundsHandler extends WordHandler {
	
	public WordSoundsHandler() {
	}
	
	public WordSoundsHandler(String soundKey) {
		Map mapWordSounds = PropertyReader.getInstance().getWordSounds();
		String value = (String) mapWordSounds.get(soundKey);
		StringTokenizer stk = new StringTokenizer(soundKey, "|");
		displayData(value, stk.nextToken());
	}
	
/*	//Get these from the action............
	private void initWS(Map mapData) {
		Iterator iter = mapData.entrySet().iterator();
		List lstStarting = new ArrayList();
		List lstEnding = new ArrayList();
		while(iter.hasNext()) {
			Map.Entry mapentry = (Map.Entry) iter.next();
			String key = (String) mapentry.getKey();
			StringTokenizer stk = new StringTokenizer(key, "|");
			String soundKey = stk.nextToken();

			if(soundKey.equals("1")) {
				//Starting
				lstStarting.add(key);
			}
			else if(soundKey.equals("2")) {
				//Ending
				lstEnding.add(key);
			}
		}
		displayData(lstStarting, lstEnding);
	}
	
	private void displayData(List lstStartingSound, List lstEndingSound) {
		StringBuffer sbDisplay = new StringBuffer();
		sbDisplay.append("<table align=\"center\" width=\"100%\" border=\"0\" >");
			sbDisplay.append("<tr>");
				sbDisplay.append("<td width=\"10%\"> &nbsp; </td>");
				sbDisplay.append("<td width=\"80%\" align=\"center\">");
				    sbDisplay.append("<fieldset><legend>Choose a starting sound</legend>");
				    sbDisplay.append(generateRadioChoices(lstStartingSound));
				    sbDisplay.append("</fieldset>");
				sbDisplay.append("</td>");
				sbDisplay.append("<td width=\"10%\"> &nbsp; </td>");
			sbDisplay.append("</tr>");
			sbDisplay.append("<tr>");
				sbDisplay.append("<td colspan=\"3\"> &nbsp;</td>");
			sbDisplay.append("</tr>");
			sbDisplay.append("<tr>");
			sbDisplay.append("<td width=\"10%\"> &nbsp; </td>");
			sbDisplay.append("<td width=\"80%\" align=\"center\">");
			    sbDisplay.append("<fieldset><legend>Choose a ending sound</legend>");
			    sbDisplay.append(generateRadioChoices(lstEndingSound));
			    sbDisplay.append("</fieldset>");
			sbDisplay.append("</td>");
			sbDisplay.append("<td width=\"10%\"> &nbsp; </td>");
		sbDisplay.append("</tr>");
	    sbDisplay.append("</table>");		
	    
	    setDisplayData(sbDisplay.toString());		
	}
	
	
	private String generateRadioChoices(List lstData) {
		Collections.sort(lstData);
		int size = lstData.size();
		
		String width = String.valueOf(size * (3+10+5)) + "%";
		
		StringBuffer sbradio = new StringBuffer("<table width=\"" + width + "\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">");
			sbradio.append("<tr>");
			for(int i=0; i<lstData.size(); i++) {
				sbradio.append("<td width=\"3%\">");
				String data = (String) lstData.get(i);
				StringTokenizer stk = new StringTokenizer(data, "|");
				String sound = stk.nextToken();
				sbradio.append("<input type=\"radio\" name=\"choice\" value=\" ");
				sbradio.append(data);
				sbradio.append("\" onclick=\"checkAnswer()\"  />");
	    	    sbradio.append("</td>");
	    	    sbradio.append("<td width=\"10%\" class=\"leftlabel\" >");
	    	    	sbradio.append(sound);
	    	    sbradio.append("</td>");
	    	    sbradio.append("<tdwidth=\"5%\"> &nbsp; </td>");
			}
			sbradio.append("</tr>");
		sbradio.append("</table>");
		
		return sbradio.toString();
	}	
	*/
	private void displayData(String value, String key) {
		
		StringBuffer sbDisplay = new StringBuffer();
		sbDisplay.append("<table align=\"center\" width=\"100%\" border=\"0\" cellspacing=\"0\" >");
			sbDisplay.append("<tr>");
				sbDisplay.append("<td width=\"10%\" class=\"tableHeader\"> " + key  + "</td>");
				
				sbDisplay.append("<td width=\"80%\" class=\"tableHeader\">");
				    sbDisplay.append("Sounded Words....");
				sbDisplay.append("</td>");
				sbDisplay.append("<td width=\"10%\" class=\"tableHeader\"> &nbsp; </td>");
			sbDisplay.append("</tr>");

			StringTokenizer stk = new StringTokenizer(value, ",");
			while(stk.hasMoreTokens()) {
				String word = stk.nextToken();
				sbDisplay.append("<tr>");
					sbDisplay.append("<td width=\"10%\"> &nbsp; </td>");
					sbDisplay.append("<td width=\"80%\" align=\"center\">");
					    sbDisplay.append(WordUtil.getWordData(word, false));
					sbDisplay.append("</td>");
					sbDisplay.append("<td width=\"10%\"> &nbsp; </td>");
				sbDisplay.append("</tr>");
				sbDisplay.append("<tr>");
				sbDisplay.append("<td colspan=\"3\"> <hr> </td>");
			sbDisplay.append("</tr>");
			}

	    sbDisplay.append("</table>");		
	    
	    setDisplayData(sbDisplay.toString());		
	}
	
	public String generateWordSoundKeys() {
		Map mapWordSounds = PropertyReader.getInstance().getWordSounds();
		Iterator iter = mapWordSounds.entrySet().iterator();
		List lstStarting = new ArrayList();
		List lstEnding = new ArrayList();
		while(iter.hasNext()) {
			Map.Entry mapentry = (Map.Entry) iter.next();
			String key = (String) mapentry.getKey();
			StringTokenizer stk = new StringTokenizer(key, "|");
			String sound = stk.nextToken();
			String soundKey = stk.nextToken();

			if(soundKey.equals("1")) {
				//Starting
				lstStarting.add(sound);
			}
			else if(soundKey.equals("2")) {
				//Ending
				lstEnding.add(sound);
			}
		}
		
		StringBuffer sbWsKeys = new StringBuffer();
		sbWsKeys.append("<table width=\"100%\" >");
			sbWsKeys.append("<tr>");
				sbWsKeys.append("<td class=\"tableHeader\">");
					sbWsKeys.append("Choose starting sounded words from below");		
				sbWsKeys.append("</td>");
			sbWsKeys.append("</tr>");
			sbWsKeys.append("<tr>");
				sbWsKeys.append("<td>");
					sbWsKeys.append(this.generateKeys(lstStarting, "starting", "processDisplay('starting')"));
					sbWsKeys.append("</td>");
			sbWsKeys.append("</tr>");
			sbWsKeys.append("<tr><td>&nbsp;</td></tr>");
			sbWsKeys.append("<tr>");
				sbWsKeys.append("<td class=\"tableHeader\">");
					sbWsKeys.append("Choose ending sounded words from below");		
				sbWsKeys.append("</td>");
		    sbWsKeys.append("</tr>");
			sbWsKeys.append("<tr>");
				sbWsKeys.append("<td>");
					sbWsKeys.append(this.generateKeys(lstEnding, "ending", "processDisplay('ending')"));
				sbWsKeys.append("</td>");
			sbWsKeys.append("</tr>");
		sbWsKeys.append("</table>");
		
		return sbWsKeys.toString();
		
	}
	
	private String generateKeys(List data, String group, String onclick) {
		StringBuffer sbWsKeyBlock = new StringBuffer();

		sbWsKeyBlock.append("<table  width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">");
		boolean countdisplay = false;
		int nCounter = 0;
		int nDataCount = 0;
        while(!countdisplay) {
        	sbWsKeyBlock.append("<tr>");
	        	for(int i=0; i<10; i++) {
	        		String value = "";	
	        		sbWsKeyBlock.append("<td  width=\"3%\">");
	        			if(nDataCount<data.size() && !countdisplay ) {
	        				value = (String) data.get(nDataCount);
	        		    	sbWsKeyBlock.append("<input type=\"radio\" name=\"" + group + "\" value=\"");
	        		    	sbWsKeyBlock.append(value);
	        		    	sbWsKeyBlock.append("\" onclick=\"");
	        		    	sbWsKeyBlock.append(onclick);
	        		    	sbWsKeyBlock.append("\"  />");
							nDataCount++;
	        		    }
	        		    else {
	        		    	sbWsKeyBlock.append("&nbsp;");
	        		    	value = "&nbsp";
	        		    	countdisplay = true;
	        		    }
	        		sbWsKeyBlock.append("</td>");
	        		sbWsKeyBlock.append("<td  width=\"5%\" class=\"leftlabel\" nowrap>");
	        		sbWsKeyBlock.append(value);
	        		sbWsKeyBlock.append("</td>");
	        		sbWsKeyBlock.append("<td width=\"2%\" >");
	        			sbWsKeyBlock.append("&nbsp;");
	        		sbWsKeyBlock.append("</td>");
	        	}
	        	nCounter = nCounter + 10;
        	sbWsKeyBlock.append("</tr>");
        	sbWsKeyBlock.append("<tr>");
        		sbWsKeyBlock.append("<td colspan=\"10\"> &nbsp; </td>");
        	sbWsKeyBlock.append("</tr>");
        }
        sbWsKeyBlock.append("</table>");      			

        return sbWsKeyBlock.toString();
    }	

}

